home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3100 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.3 KB

  1. Path: news.bridge.net!news
  2. From: David Byrden <100101.2547@compuserve.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Need help with overloading "<<"
  5. Date: 22 Jan 1996 08:47:43 GMT
  6. Organization: self-employed
  7. Message-ID: <4dvivf$460@news.bridge.net>
  8. References: <4duet0$ju9@wegener.ems.psu.edu> <yang-2101962117310001@hobbes.ece.uiuc.edu>
  9. NNTP-Posting-Host: ppp-mia1-55.bridge.net
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (Windows; I; 16bit)
  14.  
  15.  
  16. >> >I was trying to overload the "<<" operator for a Complex class,
  17. >> >without much sucess. Could someone please help me out ?
  18.  
  19.  
  20. >> you should put one line in your class definition:
  21. >>  friend ostream& operator<<(ostream& s, Complex& z);
  22.  
  23.  
  24. It does not actually need to be a friend, not in this case, but this will 
  25. solve the problem, which is that the header file gives no hint that the 
  26. operator<< function exists.
  27.  
  28. Also, you could rework your class to deal with constancy. For example, 
  29. the operator<< function could take a const reference to complex;
  30.  
  31.     ostream& operator<<(ostream& s, const Complex& z);
  32.  
  33. which will allow you to do this;
  34.  
  35.      cout  <<   Complex( 5,5 )  << endl ;
  36.  
  37.  
  38. The Complex class, similarly, should have const member functions where 
  39. appropriate.
  40.  
  41.  
  42.            David.
  43.  
  44.  
  45.  
  46.